<?php
/*************************************************

平台控数据接口 - the
Author: Wanghuabin <17-10-31>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class PlatformController extends Controller {

    //平台详情页
    public function platform_detail() {
        $plat_id = I('get.plat_id');
        $map['id'] = $plat_id;
        $plat = D('Platform')->get_one($map);
        $map = array();
        $map['platform_id'] = $plat['id'];
        $plat_obj = D('PlatformObject')->get_all($map);
        foreach ($plat_obj as $k => $v) {
        $pic = $v['pic'];
        $plat_obj[$k]['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);
        }
        
    
        $plat_map = M('PlatformMap')->where($map)->find();
        $plat['plat_map'] = $plat_map['pic'];
        $plat_team = M('PlatformTeam')->where($map)->select();
        foreach ($plat_team as $k => $v) {
             $pic = $v['pic'];
             $plat_team[$k]['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);
        }
        $plat['plat_team'] = $plat_team;
        $plat_assets = M('PlatformAssets')->where($map)->select();
        $plat['plat_assets'] = $plat_assets;
        $video_app = M('VideoAppraisal')->where($map)->select();
        //print_r($video_app);die;
        // $plat['video_app'] = $plat_assets;
        if($video_app){
            foreach($video_app as $k => $v) {
                $house_id[] = $v['house_id'];
                $house_id_arr = $v['house_id'];
                $cate_id[$house_id_arr] = $v['cate_id'];
            }
           // print_r($cate_id);die;
            $map['id'] = array('in',$house_id);
            $video = D('Video')->get_all($map);
            foreach ($video as $k => $v) {
             $pic = $v['pic'];
             $video[$k]['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);
            }

            if($video){
                foreach ($video as $k => $v) {
                    $vid = $v['id'];
                    $video[$k]['cate_id'] = $cate_id[$vid];
                    if($v['id'] == 574){
                       unset($video[$k]);
                    }
                }
                $plat['video'] = $video;
                $plat['news'] = 0;
            }else{
                $plat['video'] = array();
                 $plat['news'] = 1;
            }
        }else{
            $plat['video'] = array();
            $plat['news'] = 0;
        }
        if($plat){
            $res['errorCode'] = 1;
            $res['data']['paltform'] = $plat;
            $res['data']['plat_obj'] = $plat_obj;
        }else{
             $res['errorCode'] = 0;
             $res['msg'] ="未找到平台";
        }
        $this->ajaxReturn($res);
    }

    //果樹賬號綁定
     public function gs_bind_user(){
        $id = I('post.id');
        import("Api.Guoshu");
        $gs = new \Guoshu();
        $info = $gs->login();
        $login_info = json_decode($info,true);
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            $phone = I('post.phone');
            $realname =I('post.realname');
            $idcard =I('post.idcard');
            $user_id = I('post.userid');

            $r = $gs->bind_user($token,$phone,$realname,$idcard,$user_id);
            $r = json_decode($r,true);
            //返回信息开始登陆
             //返回信息开始登陆
            if($r['status']==200 or $r['status']==201){
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = 8;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat  && $user_id){
                    $map = array();
                    $map['id'] = $user_id;
                    $user_info = D('User')->where($map)->find();
                        $data['user_id'] = $user_id;
                        $data['add_time'] = time();
                        $data['platform_id'] = 8;
                        $data['referral'] =$user_info['referral'];
                        $map = array();
                        $map['platform_id'] = 8;
                        $map['user_id'] = $user_id;
                        $res =  D('UserPlatform')->where($map)->find();
                           if(!$res){
                                D('UserPlatform')->add($data);
                           }
                }
                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'user_id'=>$user_id);
                //print_r($data);
                $rsa_data = $this->rsa_privencrypt($data);
                // print_r($rsa_data);die;
                $url = 'https://www.goodsure.cn/jiekou/8hjie/index.php?token='.$token.'&datakey=login';
                if($r['status']==200){
                    $data = array('errorCode'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']==201){
                    $data = array('errorCode'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            } 

        }
      
    }
    
    //万盈账号绑定
    public function wy_bind_user(){
        $id = I('post.id');
        import("Api.Wanying");
        $wy = new \Wanying();
        $info = $wy->login();

        $login_info = json_decode($info,true);
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            $phone = $_SESSION['user']['user_phone'];
            $realname = $_SESSION['user']['realname'];
            $idcard = $_SESSION['user']['idcard'];
            $userid = $_SESSION['user']['id'];

            $r = $wy->bind_user($token,$phone,$realname,$idcard,$userid);
           // print_r($r);
            $r = json_decode($r,true);
            if($r['status']==200 or $r['status']=="-101" or $r['status']=="-103"){
                $map = array();
                $map['user_id'] = $userid;
                $map['platform_id'] = 2;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat && $userid){
                    $map = array();
                    $map['id'] = $userid;
                    $user_info = D('User')->where($map)->find();

                    $data['user_id'] = $userid;
                    $data['add_time'] = time();
                    $data['platform_id'] = 2;
                    $data['referral'] =$user_info['referral'];
                    D('UserPlatform')->add($data);
                }

                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'UserId'=>$userid);
                $rsa_data = $this->rsa_pubencrypt($data);
                $info = $wy->login();
                $login_info = json_decode($info,true);
                $token = $login_info['data']['token'];
                $url = 'https://www.wyjr168.com/ThirdParty/JinrongjieLogin?token='.$token.'&datakey=login';
               //$url="http://new.8hjie.com/Home/Platform/test";
                 if($r['status']==200 or $r['status']=='-103'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']=='-101'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            }


        }
      
    }
    
    
    //普惠账号绑定
    public function ph_bind_user(){
        $id = I('post.id');
        import("Api.Puhuiyuntong");
        $ph = new \Puhuiyuntong();
        $info = $ph->login();
        $login_info = json_decode($info,true);
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            $phone = $_SESSION['user']['user_phone'];
            $realname = $_SESSION['user']['realname'];
            $idcard = $_SESSION['user']['idcard'];
            $userid = $_SESSION['user']['id'];

            $r = $ph->bind_user($token,$phone,urlencode($realname),$idcard,$userid);
            //print_r($r);
            $r = json_decode($r,true);
            if($r['Status']==200 or $r['Status']==202 or $r['Status']=="1003" or $r['Status']=="201"){

                $map = array();
                $map['user_id'] = $userid;
                $map['platform_id'] = 2;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat && $userid){
                    $map = array();
                    $map['id'] = $userid;
                    $user_info = D('User')->where($map)->find();

                    $data['user_id'] = $userid;
                    $data['add_time'] = time();
                    $data['platform_id'] = 2;
                    $data['referral'] =$user_info['referral'];
                    D('UserPlatform')->add($data);
                }

                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'UserId'=>$userid);
                $rsa_data = $this->rsa_pubencrypt($data);
                $info = $ph->login();
                $login_info = json_decode($info,true);
                $token = $login_info['data']['token'];
                 $url = 'https://www.phyt88.com/v3/finance/dologin?token='.$token.'&datakey=login';
               //$url="http://new.8hjie.com/Home/Platform/test";
                 if($r['Status']==200 or $r['Status']==202 or $r['Status']=='1003'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['Status']=='201'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            }


        }
      
    }

    

    
    
        //私钥加密公钥解密
    public function rsa_privencrypt($data){
        $id = I('post.id');
        import("Api.Rsa");
        $data = json_encode($data);
         $pubKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_public_key.pem'; 
        $privKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_private_key.pem';
        
        import("Api.Rsa");
        $m = new \Rsa($pubKey, $privKey);
        $x = $m->privEncrypt($data); 
        $x = $m->privEncrypt($data); 
        return($x);
       
    }


    //公钥加密公私钥解密

    public function rsa_pubencrypt($data){
        $id = I('post.id');
        import("Api.Rsa"); 
        $data = json_encode($data);
        ////////////////////////////////////// 
        $pubKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_public_key.pem'; 
        $privKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_private_key.pem';
        // $m = new RSA($pubfile, $prifile); 
        $m = new \Rsa($pubKey, $privKey);
        $x = $m->pubEncrypt($data); 
        return($x);
       
    }
    
     //我要评测平台
    public function plat_qusetion() {
        if(IS_POST){
            $postdata = I('post.');
            if($postdata['plat_name']) {
                $postdata['plat_invest'] = implode(',',$postdata['plat_invest']);
                $postdata['plat_bad'] = implode(',',$postdata['plat_bad']);
                $res = M('PlatformQuestion')->add($postdata);
                if($res) {
                    $data = array('err'=>0,'msg'=>"提交成功");
                } else {
                    $data = array('err'=>1,'msg'=>"提交失败");
                }
                $this->ajaxReturn($data);
            } else {
                $data = array('err'=>2,'msg'=>"平台名称不能为空");
            }
        } else {
            $this->display();
        }
    }




    //app跳转平台token
    public function get_app_token(){
        $userid = I('post.userid');
        $map['id'] = $userid;
        $token = substr(md5(time()), 8, 16);
        $data['app_token'] = $token;
        $user_login = D('User')->edit($map,$data);
        if($user_login){
            $data = array('errorCode'=>1,'msg'=>"记录成功！",'token'=>$token);
        }else{
            $data = array('errorCode'=>0,'msg'=>"记录失败！");
        }
        $this->ajaxReturn($data);
    }
    

}